const mongoose = require('mongoose')
const Schema = mongoose.Schema

const blogCollection = new Schema(
  {
    id: {type: Schema.Types.UUID},
    title: { type: String, required: true },
    description: { type: String, required: true },
    toc:{ type: String, required: true },
    htmlContent: { type: String, required: true },
    thumb: { type: String, required: false },
    scanNumber: { type: Number, required: true },
    commentNumber: { type: Number, required: true },//文章评论数
    createDate: { type: String, required: true },
    categoryId: {
      type: Schema.Types.ObjectId
    },
    // 文章的点赞数
    likenum:{type:Number},
    //存放文章markdown语句的文件路径，用于编辑文章时读取
    fileMarkdown:{type:String},
    //该文章管理员还没有回复的评论数
    adminNoCommentNumber:{type:Number}
  },
  {
    toObject: { virtuals: true }
  }
)
blogCollection.methods.toJSON = function () {
  const obj = mongoose.Document.prototype.toJSON.call(this);
  const categoryType = typeof obj.categoryId;
  if (categoryType === 'object') {
    if (obj.categoryId) {
      obj.category = obj.categoryId;
      obj.categoryId = obj.categoryId.id;
    } else {
      obj.category = null;
    }
  }
  obj.id = obj._id;
  return obj;
};
const Blog = mongoose.model('Blog', blogCollection)

module.exports = Blog